<?php

namespace App\Module\Record;

use App\Module\BaseModule;
use App\Service\DictService;
use App\depository\RecordFriendKillDep;
use App\depository\SteamIDEosidDep;

class FriendkillModule extends BaseModule
{
    public function init($request)
    {
        $dictService = new DictService();
        $dictService->setTeams();

        $data = [
            'dict' => $dictService->getDict(),
        ];

        return self::response($data);
    }


    public function add($request)
    {
        $param = $request->input();

        $dep = new RecordFriendKillDep();
        $mapDep = new SteamIDEosidDep();

        $data = [
            'date' => $param['date'],
            'gh_id' => $param['gh_id'],
            'murderer_name' => $param['murderer_name'],
            'murderer_steamid' => $param['murderer_steamid'],
            'victim_name' => $param['victim_name'],
            'victim_steamid' => $param['victim_steamid']
        ];

        $dep->add($data);

        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();

        $dep = new RecordFriendKillDep();

        $dep->del($param['id']);
        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        $dep = new RecordFriendKillDep();
        $mapDep = new SteamIDEosidDep();

        $data = [
            'date' => $param['date'],
            'gh_id' => $param['gh_id'],
            'murderer_name' => $param['murderer_name'],
            'murderer_steamid' => $param['murderer_steamid'],
            'victim_name' => $param['victim_name'],
            'victim_steamid' => $param['victim_steamid']
        ];

        $dep->upd($param['id'], $data);

        return self::response();

    }

    public function list($request)
    {
        $param = $request->input();
        $teamId = $request->headers->get('TeamId');

        $param['per_page'] = $param['per_page'] ?? 50;
        $param['current_page'] = $param['current_page'] ?? 1;
        $param['gh_id'] = $teamId;

        $dep = new RecordFriendKillDep();
        $resList = $dep->list($param);

        $data['list'] = $resList->map(function ($item) {
            return [
                'id' => $item->id,
                'gh_id' => $item->gh_id,
                'date' => $item->date,

                'murderer_name' => $item->murderer_name,
                'murderer_steamid' => $item->murderer_steamid,
                'victim_name' => $item->victim_name,
                'victim_steamid' => $item->victim_steamid,
            ];
        });

        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];

        return self::response($data);
    }

}

